package com.hllxd.hism.star.basic.service.service;


import com.hllxd.hism.star.basic.service.dao.mapper.doris.AlertDashboardMapper;
import com.hllxd.hism.star.basic.service.model.AlertDashboardEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;


@Slf4j
@Service("alertDashboardService")
public class AlertDashboardServiceImpl implements AlertDashboardService {

    @Autowired
    AlertDashboardMapper alertDashboardSMapper;


    @Override
    public List<AlertDashboardEntity> listTopicDashboard(HashMap<String, String> paramMap) {
        String inteval = paramMap.get("interval");
        String[] dateInterval = handleInterval(inteval);
        paramMap.put("startDt", dateInterval[0]);
        paramMap.put("endDt", dateInterval[1]);
        return alertDashboardSMapper.listTopicDashboard(paramMap);
    }

    @Override
    public List<AlertDashboardEntity> listRuleDashboard(HashMap<String, String> paramMap) {
        String inteval = paramMap.get("interval");
        String[] dateInterval = handleInterval(inteval);
        paramMap.put("startDt", dateInterval[0]);
        paramMap.put("endDt", dateInterval[1]);
        return alertDashboardSMapper.listRuleDashboard(paramMap);
    }

    private String[] handleInterval(String interval) {
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(1);
        switch (interval) {
            case "week":
                LocalDateTime weekLDT = localDateTime.minusDays(7);
                String weekST = weekLDT.getYear() + "-" + weekLDT.getMonthValue() + "-" + weekLDT.getDayOfMonth();
                String weekET = localDateTime.getYear() + "-" + localDateTime.getMonthValue() + "-" + localDateTime.getDayOfMonth();
                return new String[]{weekST, weekET};
            case "month":
                LocalDateTime monthLDT = localDateTime.minusDays(30);
                String monthST = monthLDT.getYear() + "-" + monthLDT.getMonthValue() + "-" + monthLDT.getDayOfMonth();
                String monthET = localDateTime.getYear() + "-" + localDateTime.getMonthValue() + "-" + localDateTime.getDayOfMonth();
                return new String[]{monthST, monthET};
            case "day":
            default:
//                String startTime = dorisMapper.getMaxDt();
                String startTime = localDateTime.getYear() + "-" + localDateTime.getMonthValue() + "-" + localDateTime.getDayOfMonth();
                return new String[]{startTime, startTime};
        }
    }
}
